package com.example.otto.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.otto.Jwt.JwtUtil;
import com.example.otto.entity.Task;
import com.example.otto.mapper.TaskMapper;
import com.example.otto.service.TaskService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController {

    JwtUtil jwtUtil = new JwtUtil();

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private TaskService taskService;

    //查看所有报修单
    @GetMapping
    public ResponseEntity<List<Task>> findAll(@RequestHeader(name = "Authorization", required = false) String authHeader) {

        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        List<Task> all = taskMapper.findAll();

        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    //提交报修单
    @PostMapping
    public ResponseEntity<Boolean> add(@RequestBody Task task,
                                 @RequestHeader(name = "Authorization", required = false) String authHeader){

        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(taskService.submitTask(task), HttpStatus.OK);
    }

    //修改报修单
    @PutMapping
    public ResponseEntity<Integer> change(@RequestBody Task task,
                          @RequestHeader(name = "Authorization", required = false) String authHeader){

        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(taskMapper.update(task), HttpStatus.OK);
    }

    //分页查询报修单
    @GetMapping("/page")
    public ResponseEntity<IPage<Task>> findPage(@RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize,
                                                @RequestParam(defaultValue = "", value = "name", required = false) String name,
                                                @RequestParam(defaultValue = "", value = "date", required = false) String date,
                                                @RequestParam(defaultValue = "", value = "state", required = false) String state,
                                                @RequestHeader(name = "Authorization", required = false) String authHeader) {
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        // 接下来，执行原来的查询逻辑
        IPage<Task> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        queryWrapper.like("date", date); // 注意：如果你的date字段是日期类型，你可能需要转换这个字符串
        queryWrapper.like("state", state);
        IPage<Task> taskPage = taskService.page(page, queryWrapper);

        return new ResponseEntity<>(taskPage, HttpStatus.OK);
    }

    //报修人查询自己的报修单
    @GetMapping("/user_search")
    public ResponseEntity<IPage<Task>> user_find_task(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam Integer id,
                                @RequestParam(defaultValue = "", value = "state",required = false) String state,
                                      @RequestHeader(name = "Authorization", required = false) String authHeader ){

        Claims claims = jwtUtil.Verify(authHeader);
        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        IPage<Task> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("userId", id);
        queryWrapper.like("state", state);
        IPage<Task> taskPage = taskService.page(page, queryWrapper);

        return new ResponseEntity<>(taskPage, HttpStatus.OK);
    }

    //维修人员查看自己负责的维修单
    @GetMapping("/worker_search")
    public ResponseEntity<Task> worker_find_task(@RequestParam Integer id,
                                                 @RequestHeader(name = "Authorization", required = false) String authHeader){

        Claims claims = jwtUtil.Verify(authHeader);
        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(taskMapper.workerView(id), HttpStatus.OK);
    }

//    @GetMapping("/page/find_by_name")
//    public List<Task> findByName(@RequestParam String name){
//        return taskMapper.findByName(name);
//    }
//
//    @GetMapping("/page/find_by_date")
//    public List<Task> findByDate(@RequestParam("date") Date date){
//        return taskMapper.findByDate(date);
//    }

    //确认报修单完成
    @PutMapping("/confirm")
    public ResponseEntity<Boolean> confirm_task(@RequestParam Integer num,
                                                @RequestHeader(name = "Authorization", required = false) String authHeader){
        Claims claims = jwtUtil.Verify(authHeader);
        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(taskService.confirmTask(num), HttpStatus.OK);

    }

    @GetMapping("/page/find_by_date_name")
    public List<Task> findByDate_Name(@RequestParam(value = "name",required = false) String name,@RequestParam(value =
            "date", required = false) Date date){
        return taskService.findByDate_Name(name, date);
    }

    //修改报修单
    @PutMapping("/page")
    public ResponseEntity<Boolean> alterTask(@RequestBody Task task,
                                             @RequestHeader(name = "Authorization", required = false) String authHeader){
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(taskService.alterTask(task), HttpStatus.OK);
    }

    //通过id获取维修人员的历史订单
    @GetMapping("/worker_history_task")
    public ResponseEntity<IPage<Task>> workerHistoryTask(@RequestParam Integer pageNum,
                                                        @RequestParam Integer pageSize,
                                                        @RequestParam Integer id,
                                                        @RequestHeader(name = "Authorization", required = false) String authHeader){
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        IPage<Task> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("workerId", id);
        queryWrapper.like("state", 2);
        IPage<Task> taskPage = taskService.page(page, queryWrapper);

        return new ResponseEntity<>(taskPage, HttpStatus.OK);
    }

    //通过id获取维修人员处理中的订单
    @GetMapping("/worker_handling_task")
    public ResponseEntity<Task> workerHandlingTask(@RequestParam Integer id,
                                                        @RequestHeader(name = "Authorization", required = false) String authHeader){
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(taskMapper.findHandlingTaskByWorkerId(id),HttpStatus.OK);
    }


}
